//
// Created by Administrator on 2021/8/3.
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>

using namespace std;

class Solution {
public:
    int findPoisonedDuration(vector<int> &timeSeries, int duration) {
        int ans = 0;
        int endTime = -1;
        int startTime = -1;
        for (int timeSerie : timeSeries) {
            if (timeSerie > endTime) { // 上一次攻击的效果已经结束
                ans += endTime - startTime;
                startTime = timeSerie;
            }
            endTime = timeSerie + duration; // 更新这次攻击的状态结束时间
        }
        ans += endTime - startTime;
        return ans;
    }
};

class Solution2 {
public:
    int findPoisonedDuration(vector<int> &timeSeries, int duration) {
        if (timeSeries.empty())
            return 0;
        int ans = 0;
        for (int i = 0; i < timeSeries.size() - 1; ++i) {
            ans += min(duration, timeSeries[i + 1] - timeSeries[i]);
        }
        return ans + duration; // 加上最后一次
    }
};

int main() {
    vector<int> timeSeries{1, 2};
    Solution2 solution;
    cout << solution.findPoisonedDuration(timeSeries, 2) << endl;
    return 0;
}
